package ch.rheintalconnection.sysventory.businessServices;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ch.rheintalconnection.sysventory.domain.*;
import ch.rheintalconnection.sysventory.technicalServices.DataValidator;
import ch.rheintalconnection.sysventory.technicalServices.IDataReader;

public class DataService {

	IDataReader dataReader;

	public DataService(IDataReader dataReader) {
		super();
		this.dataReader = dataReader;
	}

	// UC3
	// UC5 (2.3)
	public List<ScanEntry> getFilteredNewestScanEntries(
			String filterComputerName, String filterIpAddress,
			String filterProcessorFamily, String filterComputerType,
			String filterOsDescription) {

		List<Client> clients = dataReader.selectAllClients();
		List<ScanEntry> scanEntries = new ArrayList<ScanEntry>();

		for (Client client : clients) {
			ScanEntry scanEntry = dataReader.selectNewestScanEntry(client);
			if (scanEntry != null) {
				scanEntries.add(scanEntry);
			}
		}

		List<ScanEntry> filteredScanEntries = new ArrayList<ScanEntry>();

		if (scanEntries.size() <= 0) {
			return filteredScanEntries;
		}

		String filterComputerNameRegex = filterComputerName.replace("*", ".*")
				.replace("?", ".");
		String filterIpAddressRegex = filterIpAddress.replace("*", ".*")
				.replace("?", ".");
		String filterProcessorFamilyRegex = filterProcessorFamily.replace("*",
				".*").replace("?", ".");
		String filterComputerTypeRegex = filterComputerType.replace("*", ".*")
				.replace("?", ".");
		String filterOsDescriptionRegex = filterOsDescription
				.replace("*", ".*").replace("?", ".");

		DataValidator dataValidator = new DataValidator();

		for (ScanEntry scanEntry : scanEntries) {
			if (dataValidator.vertifyScanEntry(scanEntry,
					filterComputerNameRegex, filterIpAddressRegex,
					filterProcessorFamilyRegex, filterComputerTypeRegex,
					filterOsDescriptionRegex)) {
				filteredScanEntries.add(scanEntry);
			}
		}
		return filteredScanEntries;
	}

	
	// UC5 (2.1)
	@SuppressWarnings("deprecation")
	public int getScanQty(Date fromDate) {
		int c = 0;
		List<Scan> scans = dataReader.selectAllScans();
		for (Scan scan : scans) {
			if (scan.getEndDate().getDate() > fromDate.getDate()) {
				c++;
			}
		}
		return c;
	}

	// UC5 (2.4)
	@SuppressWarnings("deprecation")
	public List<ScanEntry> getScanEntriesFromChangedComputers(Date fromDate, Date tillDate) {
		List<Client> clients = dataReader.selectAllClients();

		List<ScanEntry> changedScanEntries = new ArrayList<ScanEntry>();

		for (Client client : clients) {
			List<ScanEntry> scanEntries = dataReader.selectScanEntries(client);
			Boolean hasChanged = false;
			ScanEntry oldValue = null;

			for (ScanEntry scanEnrty : scanEntries) {
				if (scanEnrty.getScandate().getDate() > tillDate.getDate()) {
					continue;
				}
				if (scanEnrty.getScandate().getDate() < fromDate.getDate()) {
					break;
				}
				if (!scanEnrty.equals(oldValue)) {
					hasChanged = true;
					break;
				}
				oldValue = scanEnrty;
			}
			if (hasChanged) {
				changedScanEntries.add(dataReader
						.selectNewestScanEntry(client));
			}
		}

		return changedScanEntries;
	}

}
